package com.example.administrator.a0015sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.a0015sqlite.db.MyopenDb;

public class MainActivity extends AppCompatActivity {
    private MyopenDb myopendb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myopendb = new MyopenDb(this,"my_sqlite_db",null,1);
    }
    public void onClick(View v){
        String msg;
        switch (v.getId()){
            case R.id.createDb :
                myopendb.getWritableDatabase();
                msg = "创建数据成功";
                break;
            case R.id.createTable:
                 SQLiteDatabase db = myopendb.getWritableDatabase();
                 db.execSQL("drop table if exists book");
                 db.execSQL("create table book(id integer primary key autoincrement,author text,price real,name text)");
                msg = "创建表成功";
                break;
            case R.id.create:
                SQLiteDatabase cdb = myopendb.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("author","大喵");
                contentValues.put("price",100.23);
                contentValues.put("name","冬天里的一把火");
                cdb.insert("book",null,contentValues);
                contentValues.clear();
                contentValues.put("author","小苗");
                contentValues.put("price",10);
                contentValues.put("name","冬天里的火");
                cdb.insert("book",null,contentValues);
                contentValues.clear();
                msg = "添加了两条数据";
                break;
            case R.id.update:
                    SQLiteDatabase udb = myopendb.getWritableDatabase();
                ContentValues ucontentValues = new ContentValues();
                ucontentValues.put("author","大大喵");
                udb.update("book",ucontentValues,"author=?",new String[]{"小苗"});
                ucontentValues.clear();
                msg = "更新了一条数据";
                break;
            case R.id.query:
                String str = "没有数据";
                SQLiteDatabase db_query = myopendb.getReadableDatabase();
                Cursor cursor = db_query.query("book",null,null,null,null,null,null);
                if( cursor.moveToNext() ){
                    while (cursor.moveToNext()){
                        int id = cursor.getInt(cursor.getColumnIndex("id"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        Double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        str += "id:"+id+",author:"+author+",price:"+price+",name:"+name+"\n";
                    }
                }
                msg = str;
                break;
            case R.id.delete:
                SQLiteDatabase dedb = myopendb.getWritableDatabase();
                dedb.delete("book","author=?",new String[]{"小苗"});
                msg = "删除了一条数据";
                break;
            default:
                msg = "什么也没发生";
        }
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
